#include "logic.h"
#include "net/CTSocket.h"

using namespace CTLib;

#define BUFF_LENGTH 10240

int Usage()
{
	printf("./logic_test username\n");
	return 0;
}

int main(int argc, char *argv[])
{
	if (argc < 2)
	{
		return Usage();
	}

	CTSocket sSocket;
	int iRet = 0;
	iRet = sSocket.Connect("192.168.1.254",9800);
	if (iRet != 0)
	{
		printf("Connect error!\n");
		return -1;
	}

	char buff[BUFF_LENGTH] = {0};

	PBMsgHead			Msghead;
	CTask<MsgReqGetTaskList> 	stReqTaskList;
	CTask<MsgReqAcceptTask>	 	stReqAcceptTask;
	CTask<MsgReqCommitTask> 	stReqCommitTask;
	CTask<MsgAnsGetTaskList> 	stAnsTaskList;

	CTask<MsgReqLogin> 	stReqLogin;
	CTask<MsgAnsLogin> 	stAnsLogin;

	char c = 0;
	bool is_login = false;
	while( (c=getchar()) )
	{
		switch(c)
		{
		case 'l':
		{/*{{{*/
			int iDataLength = 0;
			int iSendlen = 0;
			int iReqArgv[32] = {0};

			iRet = stReqLogin.DoReqHeadBody(Msghead, iReqArgv, argv[1]);
			if (iRet<0)
			{
				printf("DoReqHeadBody error!\n");
				return iRet;
			}

			memset(buff, 0, sizeof(buff) );
			iRet = stReqLogin.Encode(Msghead, buff, iDataLength);
			if (iRet<0)
			{
				printf("Encode error!\n");
				return -1;
			}

			iRet = sSocket.SendEx(buff, iDataLength, iSendlen);
			if (iRet != 0)
			{
				printf("SendEx error!\n");
				return -1;
			}

			memset(buff, 0, sizeof(buff) );
			sSocket.Recv(buff, BUFF_LENGTH, iSendlen);
			
			iRet = stAnsLogin.Decode(Msghead, buff, iDataLength);
			if (iRet!=0)
			{
				printf("Decode error!\n");
				return -1;
			}

			iRet = stAnsLogin.OutputMsg(Msghead);
			if (iRet!=0)
			{
				printf("OutputMsg error!\n");
				return -1;
			}
			is_login = true;
		}/*}}}*/
			break;
		case 'r': // get task list
		{/*{{{*/
			int iDataLength = 0;
			int iSendlen = 0;
			if( !is_login )
			{
				cout << "not login, need login first." << endl;
				continue;
			}
			int iReqArgv[32] = {0};

			iReqArgv[0] = stAnsLogin.GetPtr()->gid();

			iRet = stReqTaskList.DoReqHeadBody(Msghead, iReqArgv, "");
			if (iRet<0)
			{
				printf("DoReqHeadBody error!\n");
				return iRet;
			}

			memset(buff, 0, sizeof(buff) );
			iRet = stReqTaskList.Encode(Msghead, buff, iDataLength);
			if (iRet<0)
			{
				printf("Encode error!\n");
				return -1;
			}

			iRet = sSocket.SendEx(buff, iDataLength, iSendlen);
			if (iRet != 0)
			{
				printf("SendEx error!\n");
				return -1;
			}

			memset(buff, 0, sizeof(buff) );
			sSocket.Recv(buff, BUFF_LENGTH, iSendlen);
			
			iRet = stAnsTaskList.Decode(Msghead, buff, iDataLength);
			if (iRet!=0)
			{
				printf("Decode error!\n");
				return -1;
			}

			iRet = stAnsTaskList.OutputMsg(Msghead);
			if (iRet!=0)
			{
				printf("OutputMsg error!\n");
				return -1;
			}
		}/*}}}*/
			break;
		case 'p': // accept task
		{/*{{{*/
			int iDataLength = 0;
			int iSendlen = 0;
			if( !is_login )
			{
				cout << "not login, need login first." << endl;
				continue;
			}
			int iReqArgv[32] = {0};

			iReqArgv[0] = stAnsLogin.GetPtr()->gid();
			iReqArgv[1] = 1;//accept task id
			iReqArgv[2] = 1;//accept npc id

			iRet = stReqAcceptTask.DoReqHeadBody(Msghead, iReqArgv, "");
			if (iRet<0)
			{
				printf("DoReqHeadBody error!\n");
				return iRet;
			}

			memset(buff, 0, sizeof(buff) );
			iRet = stReqAcceptTask.Encode(Msghead, buff, iDataLength);
			if (iRet<0)
			{
				printf("Encode error!\n");
				return -1;
			}

			iRet = sSocket.SendEx(buff, iDataLength, iSendlen);
			if (iRet != 0)
			{
				printf("SendEx error!\n");
				return -1;
			}

			memset(buff, 0, sizeof(buff) );
			sSocket.Recv(buff, BUFF_LENGTH, iSendlen);
			
			iRet = stAnsTaskList.Decode(Msghead, buff, iDataLength);
			/*if (iRet!=0)
			{
				printf("Decode error!\n");
				return -1;
			}

			iRet = stAnsTaskList.OutputMsg(Msghead);
			if (iRet!=0)
			{
				printf("OutputMsg error!\n");
				return -1;
			}*/
		}/*}}}*/
			break;
		case 'c': // commit task
		{/*{{{*/
			int iDataLength = 0;
			int iSendlen = 0;
			if( !is_login )
			{
				cout << "not login, need login first." << endl;
				continue;
			}
			int iReqArgv[32] = {0};

			iReqArgv[0] = stAnsLogin.GetPtr()->gid();
			iReqArgv[1] = 1;//commit task id
			iReqArgv[2] = 2;//commit npc id

			iRet = stReqCommitTask.DoReqHeadBody(Msghead, iReqArgv, "");
			if (iRet<0)
			{
				printf("DoReqHeadBody error!\n");
				return iRet;
			}

			memset(buff, 0, sizeof(buff) );
			iRet = stReqCommitTask.Encode(Msghead, buff, iDataLength);
			if (iRet<0)
			{
				printf("Encode error!\n");
				return -1;
			}

			iRet = sSocket.SendEx(buff, iDataLength, iSendlen);
			if (iRet != 0)
			{
				printf("SendEx error!\n");
				return -1;
			}

			memset(buff, 0, sizeof(buff) );
			sSocket.Recv(buff, BUFF_LENGTH, iSendlen);
			
			iRet = stAnsTaskList.Decode(Msghead, buff, iDataLength);
			/*if (iRet!=0)
			{
				printf("Decode error!\n");
				return -1;
			}

			iRet = stAnsTaskList.OutputMsg(Msghead);
			if (iRet!=0)
			{
				printf("OutputMsg error!\n");
				return -1;
			}*/
		}/*}}}*/
			break;
		default:
			continue;
		}
	}

	return 0;
}


